<?php
namespace app\credit\service;
/**
 * 订单操作
 */
class OrderService extends \app\base\service\BaseService {

    private $model = 'credit/CreditOrder';

    /**
     * 添加购物车
     * @param $userId
     * @param $data
     * @return bool
     */
    public function addCart($userId, $data) {
        if(empty($data['pro_id']) || empty($data['qty'])) {
            return $this->error('商品参数有误!');
        }
        $proInfo = target('credit/CreditProducts')->getCreditInfo($data['pro_id']);
        if(empty($proInfo) || !$proInfo['status']) {
            return $this->error('该商品不存在!');
        }
        if($proInfo['up_time']) {
            if($proInfo['up_time'] > time()) {
                return $this->error('该商品还未上架!');
            }
        }
        if($proInfo['down_time']) {
            if($proInfo['down_time'] < time()) {
                return $this->error('该商品已下架!');
            }
        }
        if(empty($proInfo['store'])) {
            return $this->error('该商品库存不足！');
        }

        if($proInfo['store'] < $data['qty']) {
            return $this->error('该商品库存不足!');
        }
        $cartData = [];
        $cartData['currency'] = [
            'credit' => [
                'amount' => $proInfo['buy_point'],
                'decimal' => 0
            ]
        ];
        $cartData['item_no'] = $proInfo['products_no'];
        $cartData['app'] = 'credit';
        $cartData['app_id'] = $proInfo['credit_id'];
        $cartData['id'] = $proInfo['products_id'];
        $cartData['qty'] = $data['qty'];
        $cartData['price'] = $proInfo['buy_price'];
        $cartData['name'] = $proInfo['title'];
        $cartData['options'] = $proInfo['spec_data'];
        $cartData['image'] = $proInfo['image'];
        $cartData['weight'] = $proInfo['weight'];
        $cartData['url'] = url('credit/info/index', ['id' => $proInfo['credit_id']]);
        $cartData['comment_status'] = 0;
        $cartData['delivery_type'] = 1;  //需要物流
        $cartData['pay_type'] = 0; //货到付款支持
        if(!target('order/Cart', 'service')->add($userId, $cartData)) {
            return $this->error(target('order/Cart', 'service')->getError());
        }
        return $this->success();
    }

    /**
     * 刷新购物车
     * @param $userId
     * @param $data
     * @return bool
     */
    public function refreshCart($userId, $data) {
        if(empty($data)) {
            return $this->success();
        }

        $ids = [];
        foreach($data as $key => $vo) {
            $ids[] = $vo['id'];
        }
        $where = [];
        $where['_sql'] = 'products_id in (' . implode(',', $ids) . ')';
        $proList = target('credit/CreditProducts')->loadList($where);
        if(empty($proList)) {
            return $this->error('部分商品已过期!');
        }

        $proData = [];
        foreach($proList as $vo) {
            $proData[$vo['products_id']] = $vo;
        }
        $storeError = 0;
        $priceError = 0;
        $cartData = [];

        foreach($data as $key => $vo) {

            $cart = [];
            if($vo['qty'] > $proData[$vo['id']]['store']) {
                $storeError = 1;
                $cart['qty'] = 1;
            }
            if($vo['price'] <> $proData[$vo['id']]['buy_price']) {
                $priceError = 1;
                $cart['price'] = $proData[$vo['id']]['buy_price'];
            }

            if($vo['currency']['credit']['amount'] <> $proData[$vo['id']]['buy_point']) {
                $priceError = 1;
                $cart['currency']['credit']['amount'] = $proData[$vo['id']]['buy_point'];
            }

            if($cart) {
                $cart['rowid'] = $key;
                $cartData[] = $cart;
            }
        }
        if(!empty($cartData)) {
            if(!target('order/Cart', 'service')->update($userId, $cartData)) {
                return $this->error(target('order/Cart', 'service')->getError());
            }
            if($storeError && $priceError) {
                return $this->error('您购买的产品由于库存和价格更改,请重新提交订单!');
            }
            if($storeError) {
                return $this->error('您购买的产品由于库存不足,请重新提交订单!');
            }
            if($priceError) {
                return $this->error('您购买的产品由于价格更改,请重新提交订单!');
            }
        }
        return $this->success();
    }

    /**
     * 提交订单
     * @param array $data
     * @return bool
     */
    public function addOrder($data = [], $goodsData = []) {
        if(empty($data) || empty($goodsData)) {
            return $this->error('订单数据为空!');
        }
        $id = target($this->model)->add([
            'order_id' => $data['order_id']
        ]);
        if(!$id) {
            return $this->error(target($this->model)->getError());
        }
        $qty = 0;
        foreach ($goodsData as $vo) {
            if(!target('credit/CreditProducts')->where(['products_id' => $vo['product_id']])->setDec('store', $vo['goods_qty'])) {
                return $this->error('库存处理失败！');
            }
            if(!target('credit/CreditProducts')->where(['products_id' => $vo['product_id']])->setInc('sale', $vo['goods_qty'])) {
                return $this->error('销量处理失败！');
            }
            $qty += $vo['goods_qty'];
        }
/*        if(!target('credit/Credit')->where(['credit_id' => $data['order_app_id']])->setDec('store', $qty)) {
            return $this->error('库存处理失败！');
        }
        if(!target('credit/Credit')->where(['credit_id' => $data['order_app_id']])->setInc('sale', $qty)) {
            return $this->error('销量处理失败！');
        }*/
        return $this->success($id);
    }

    /**
     * 支付订单检查
     * @param $orderList
     * @return bool
     */
    public function checkOrder($orderList) {
        return true;
    }

    /**
     * 付款成功回调
     * @param $orderList
     * @return bool
     */
    public function payOrder($orderList) {
        return true;
    }

    /**
     * 取消订单操作
     * @param $orderList
     * @return bool
     */
    public function cancelOrder($orderList) {
        return true;
    }

    /**
     * 订单发货
     * @param $orderInfo
     * @return bool
     */
    public function deliveryOrder($orderInfo) {
        return true;
    }

    /**
     * 订单完成
     * @param $orderInfo
     * @return bool
     */
    public function confirmOrder($orderInfo) {
        return true;

    }
}

